//# 哪吒脚本
using UnityEngine;

public class Nezha : BaseCharacter
{
    [Header("Combat")]
    public float attackDamage = 20f;
    public Transform attackPoint;
    public float attackRange = 0.5f;
    public LayerMask enemyLayers;

    [Header("Skills")]
    public GameObject fireSpearPrefab;
    public GameObject windRingPrefab;
    public float skillCooldown = 1f;
    private float nextSkillTime = 0f;

    [Header("Forms")]
    public bool isThreeHeadsSixArmsForm = false;
    public float formChangeCooldown = 10f;
    private float nextFormChangeTime = 0f;

    protected override void Start()
    {
        base.Start();
    }

    void Update()
    {
        // 基础移动
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(horizontalInput * moveSpeed, rb.velocity.y);

        // 转向
        if (horizontalInput > 0 && !isFacingRight)
            Flip();
        else if (horizontalInput < 0 && isFacingRight)
            Flip();

        // 跳跃
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
        }

                
        // 检查角色是否可以攻击
        if (Input.GetMouseButtonDown(0) && Time.time >= nextAttackTime)
        {
            Attack();
        }


        // 普通攻击
        if (Input.GetMouseButtonDown(0))
        {
            Attack();
        }

        // 技能
        if (Input.GetKeyDown(KeyCode.Q) && Time.time >= nextSkillTime)
        {
            FireSpearSkill();
        }
        if (Input.GetKeyDown(KeyCode.E) && Time.time >= nextSkillTime)
        {
            WindRingSkill();
        }

        // 形态切换
        if (Input.GetKeyDown(KeyCode.R) && Time.time >= nextFormChangeTime)
        {
            ToggleThreeHeadsSixArmsForm();
        }
    }

    void Attack()
    {
        animator?.SetTrigger("Attack");
        nextAttackTime = Time.time + attackCooldown; // 添加攻击冷却时间


        // 检测攻击范围内的敌人
        Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);

        foreach (Collider2D enemy in hitEnemies)
        {
            enemy.GetComponent<BaseCharacter>()?.TakeDamage(attackDamage);
        }
    }

    void FireSpearSkill()
    {
        nextSkillTime = Time.time + skillCooldown;
        animator?.SetTrigger("FireSpear");

        GameObject spear = Instantiate(fireSpearPrefab, transform.position, Quaternion.identity);
        spear.GetComponent<Projectile>()?.Initialize(transform.right * (isFacingRight ? 1 : -1));
    }

    void WindRingSkill()
    {
        nextSkillTime = Time.time + skillCooldown;
        animator?.SetTrigger("WindRing");

        GameObject ring = Instantiate(windRingPrefab, transform.position, Quaternion.identity);
        // 设置风轮环的行为
    }

    void ToggleThreeHeadsSixArmsForm()
    {
        if (Time.time >= nextFormChangeTime)
        {
            isThreeHeadsSixArmsForm = !isThreeHeadsSixArmsForm;
            nextFormChangeTime = Time.time + formChangeCooldown;

            if (isThreeHeadsSixArmsForm)
            {
                attackDamage *= 2;
                moveSpeed *= 1.5f;
            }
            else
            {
                attackDamage /= 2;
                moveSpeed /= 1.5f;
            }

            animator?.SetBool("IsThreeHeadsForm", isThreeHeadsSixArmsForm);
        }
    }

    void OnDrawGizmosSelected()
    {
        if (attackPoint == null) return;
        Gizmos.DrawWireSphere(attackPoint.position, attackRange);
    }
    
    public void TakeDamage(float damage)
    {
        health -= damage;
        if (health <= 0)
        {
            Die();
        }
    }


    void Die()
    {
        // 角色死亡逻辑
        Debug.Log("Nezha has died.");
        // 可以添加更多的死亡处理逻辑
    }
}